package com.netcombine.freewill.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.netcombine.freewill.constants.PageInfo;
import com.netcombine.freewill.lib.Paging;
import com.netcombine.freewill.manager.SessionManager;
import com.netcombine.freewill.service.Local_CommunityService;
import com.netcombine.freewill.service.UserService;
import com.netcombine.freewill.vo.Local_CommunityVo;
import com.netcombine.freewill.vo.PagingVo;

@Controller
public class Local_CommunityController{

	private static final Logger logger = LoggerFactory.getLogger(Local_CommunityController.class);

	@Autowired
	private Local_CommunityService local_communityService;
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/community/local_list")
	public String local_list(@ModelAttribute Local_CommunityVo local_communityVo, PagingVo pagingVo, HttpSession session, Model model){

		if(!SessionManager.isLogin(session)){	
	
			return PageInfo.GO_LOGIN;
		}	
		
		if(pagingVo.getPage() == 0) pagingVo.setPage(1);
		int total_cnt = local_communityService.getLocal_Community_Count(pagingVo);
		
		logger.info("total_cnt : " + Integer.toString(total_cnt));
		
		Paging paging = new Paging(pagingVo.getPage(), total_cnt, PageInfo.PAGE_SIZE, PageInfo.BLOCK_PAGE, PageInfo.PAGING_SCRIPT);
		
		if(total_cnt != 0){
			
			pagingVo.setPageSize(paging.getPageSize());
			pagingVo.setStartPosition(paging.getPageStart());
			
			List<Local_CommunityVo> localList = local_communityService.getLocal_Community_List(pagingVo);
			
			model.addAttribute("local_communityVo", local_communityVo);
			model.addAttribute("localList", localList);
			
		}
			model.addAttribute(PageInfo.CURRENT_MENU, PageInfo.MENU_USER);
			model.addAttribute("paging", paging);
			model.addAttribute("pagingVo", pagingVo);
		
			return "community/local_list";
	}
	
	@RequestMapping(value = "/community/local_view")
	public String local_view(Local_CommunityVo local_communityVo, HttpSession session, Model model){
		
		
		if(!SessionManager.isLogin(session)){
			
			return PageInfo.GO_LOGIN;			
		}		
			
			Local_CommunityVo local_CommunityInfo = local_communityService.getlocal_CommunityInfo(local_communityVo);
			
			model.addAttribute("local_CommunityInfo", local_CommunityInfo);		
			
			return "community/local_view";
	}
}